<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-02 13:18:40
 * @Last Modified time: 2023-02-02 17:35:05
 */
namespace app\common\model\user;

use frank\base\BaseModel;
use frank\traits\ModelTrait;
use app\common\enum\EnumFactory;
use app\common\services\RegionService;

/**
 * 用户地址
 * Class UserAddressModel
 * @package app\common\model\user
 */
class UserAddressModel extends BaseModel
{
    use ModelTrait;

    /**
     * 数据表主键
     */
    protected $pk = 'id';

    /**
     * 模型名称
     */
    protected $name = 'user_address';

    protected $append = ['region'];
 	
 	/**
     * 地区名称
     */
    public function getRegionAttr($value, $data)
    {
    	$RegionService = RegionService::instance();
        return [
            'province' => $RegionService->getNameById($data['province_id']),
            'city' => $RegionService->getNameById($data['city_id']),
            'region' => $data['region_id'] == 0 ? $data['district']
                : $RegionService->getNameById($data['region_id']),
        ];
    }

    /**
     * is_default
     */
    public function searchIsDefaultAttr($query, $value)
    {
        $query->where('is_default', $value);
    }

    /**
     * userid搜索器
     */
    public function searchUserIdAttr($query, $value)
    {
        if (is_array($value)) {
            $query->whereIn('user_id', $value);
        } else {
            $query->where('user_id', $value);
        }
    }

    /**
     * id搜索器
     */
    public function searchIdAttr($query, $value)
    {
        if (is_array($value)) {
            $query->whereIn('id', $value);
        } else {
            $query->where('id', $value);
        }
    }
}

